package wortubung.lang.es;

import static wortubung.lang.LanguageManager.*;
import static wortubung.lang.es.ConjugadorUtil.*;
import static wortubung.util.CommonUtils.*;

import wortubung.core.LANG;
import wortubung.inflection.CONJUGATION;
import wortubung.inflection.UnknownConjugationException;
import wortubung.inflection.VerbConjugation;
import wortubung.lang.ConjugatorBase;
import wortubung.util.ImmutableStringList;

/**
 * Singleton
 */
public class Conjugador extends ConjugatorBase {

	public static final Conjugador defaultConjugador = new Conjugador();
	
	static {
		defaultConjugador.readInitFiles(ES_EXCEPCIONES_VERBALES_FILE, ES_CONJUGACIONES_AS_IS_FILE);
	}

	public static VerbConjugation conjugate(String verb) { return defaultConjugador.getConjugation(verb); }

	@Override public LANG getLanguage() { return LANG.es; }

	@Override
	protected String[] conjugate(String verbSuffix, CONJUGATION type, boolean unused) {
		if (verbSuffix.endsWith("arse") || verbSuffix.endsWith("erse") || verbSuffix.endsWith("irse"))
			verbSuffix = chopRight(verbSuffix, 2);

		int verbEnding = -1;
		if (verbSuffix.endsWith("ar"))
			verbEnding = 0;
		else if (verbSuffix.endsWith("er"))
			verbEnding = 1;
		else if (verbSuffix.endsWith("ir"))
			verbEnding = 2;
		else
			UnknownConjugationException.badVerb(verbSuffix);

		String root = null;
		ImmutableStringList endings = null;
		switch(type) {
		case FUTURE:
			endings = FUTURO_INDICES;
			// fall thru
		case CONDITIONAL:
			if (endings == null)
				endings = CONDICIONAL_INDICES;
			root = verbSuffix;
			break;

		case PRESENT:			endings = PRESENTE_INDICES            [verbEnding]; break;
		case IMPERFECT:			endings = PRETERITO_IMPERFECTO_INDICES[verbEnding]; break;
		case SIMPLE_PAST:		endings = PRETERITO_SIMPLE_INDICES    [verbEnding]; break;
		case IMPERATIVE:		endings = IMPERATIVE_INDICES          [verbEnding]; break;
		case SUBJ_PRESENT:		endings = SUBJUNCTIVO_PRESENTE_INDICES[verbEnding]; break;
		case SUBJ_IMPERFECT:	endings = SUBJUNCTIVO_PRETERITO_IMPERFECTO_INDICES[verbEnding]; break;
		default:                return null;
		}

		if (root == null)
			root = chopRight(verbSuffix, 2);

		return appendToRoot(root, verbEnding, endings);
	}

	protected String participle(String verbSuffix, boolean isPresent) {
		if (verbSuffix.endsWith("arse") || verbSuffix.endsWith("erse") || verbSuffix.endsWith("irse"))
			verbSuffix = chopRight(verbSuffix, 2);

		String ending;
		if (verbSuffix.endsWith("ar") || verbSuffix.endsWith("arse"))
			ending = (isPresent ? "ando" : "ado");
		else
			ending = (isPresent ? "iendo" : "ido");
		return chopRight(verbSuffix, 2) + ending;
	}
}
